import request from "@/utils/request";
import request2 from "@/utils/request2";
import { BaseResponse } from "@/api/types/baseResponse";
import { EmployeeResponse as EmployeeResponse2 } from "@/api/types/employee";
import type { EmpFileUploadResponse, EmpFileUploadParams } from '@/api/types/employee';
interface EmployeeInfo {
  id: number;
  username: string;
  mobile?: string;
  workNumber?: string;
  typeOfEmployment?: string;
  // ... 其他员工字段
}

interface returnDataType {
  code: string;
  data: any;
  msg: string;
}

interface EmployeeResponse {
  code: string;
  msg: string;
  data: EmployeeInfo[];
}

export function getDepartmentInfo() {
  return request({
    url: "/account/department",
    method: "get",
  });
}

//查
export function getUserInfo(deptId: number) {
  return request({
    url: "/account/employeeinfo",
    method: "get",
    params: { dept_id: deptId },
  });
}

//增加
export const addUserInfo = (data: any) => {
  return request<returnDataType,returnDataType>({
    url: "/account/employeeinfo",
    method: "post",
    data,
  });
};
//修改
export const updateUserInfo = (data: any) => {
  return request<returnDataType,returnDataType>({
    url: "/account/employeeinfo",
    method: "put",
    data,
  });
};

//删除
export const deleteUserInfo = (data: any) => {
  return request<returnDataType,returnDataType>({
    url: "/account/employeeinfo",
    method: "delete",
    data,
  });
};

// 通过部门ID查询人员
export async function getEmployeesByDepartmentId(departmentId: number) {
  return await request<
    BaseResponse<EmployeeResponse2[]>,
    BaseResponse<EmployeeResponse2[]>
  >({
    url: "/account/employeeinfo",
    params: { dept_id: departmentId },
  });
}

//查询邮箱是否重复
export const getemail = (data: any) => {
  return request<returnDataType,returnDataType>({
    url: `/email/unique?email=${data}`,
    method: "get",
  });
};

//同步激活按钮
//查询邮箱是否重复
export const getuserStatu = (data: any) => {
  return request({
    url: `/user/status?userid=${data}`,
    method: "get",
  });
};

// 通过 Excel 文件导入员工
export async function importEmployees(file: File) {
  const formData = new FormData();
  formData.append("file", file);
  return await request<BaseResponse<{}>, BaseResponse<{}>>({
    url: "/emp/file",
    method: "POST",
    data: formData,
  });
}

//续约查询
export function getrenewalInfo(userid: any) {
  return request<returnDataType, returnDataType>({
    url: `/renewal?userid=${userid}`,
    method: "get",
  });
}

//添加续约记录
export function postrenewalInfo(data: any) {
  return request<returnDataType, returnDataType>({
    url: `/renewal`,
    method: "post",
    data: data,
  });
}

//重新入职接口
export function PostReOnboarding(data: any) {
  return request<returnDataType, returnDataType>({
    url: `/entry`,
    method: "post",
    data: data,
  });
}

// 上传套餐图片
export async function uploadMealPicture(pictureBase64: string, path: string) {
  console.log({
    avatarUrlBase64: pictureBase64,
    path: path
  });
  
    return await request2.post<BaseResponse<string>, BaseResponse<string>>("/picture", {
        avatarUrlBase64: pictureBase64,
        path: path
    })
}
// 上传套餐图片
// export async function uploadMealPicture(pictureBase64: string, path: string) {
//     // 创建一个新的 axios 实例，指定不同的 baseUrl
//     const request = axios.create({
//         baseURL: "https://www.tuancanyouwo.cn:8897", 
//         timeout: 5000
//  });

//     // 发送请求
//     return await request.post<BaseResponse<string>, BaseResponse<string>>("/shop/picture", {
//         avatarUrlBase64: pictureBase64,
//         path: path
//     });
// }
// 上传商品图片
export async function uploadShopPicture(pictureBase64: string, path: string) {
    return await request.post<BaseResponse<string>, BaseResponse<string>>("/shop/picture", {
        avatarUrlBase64: pictureBase64,
        path: path
    })
}
// 上传文件
// export async function uploadEmpFile(params: EmpFileUploadParams): Promise<EmpFileUploadResponse> {
//   return await request.put<EmpFileUploadResponse, EmpFileUploadResponse>(
//     "/account/empfile",
//     params
//   );
// }
// 上传文件
export async function uploadEmpFile(params: EmpFileUploadParams): Promise<EmpFileUploadResponse> {
  const formData = new FormData();
  formData.append('userid', params.userid);

  // 将 empfile 转换为 JSON 字符串并添加到 formData 中
  formData.append('empfile', JSON.stringify(params.empfile));

  return await request.put<EmpFileUploadResponse, EmpFileUploadResponse>(
    "/account/empfile",
    formData,
    {
      headers: {
        'Content-Type': 'multipart/form-data'
      }
    }
  );
}
// // 上传附件接口
// export async function uploadFiles(empfile: Array<{ file_name: string; file_url: string }>, userid: string) {
//   const formData = new FormData();
//   formData.append('userid', userid);
  
//   // 将文件数组逐个添加到formdata中
//   empfile.forEach((file, index) => {
//     formData.append(`empfile[${index}][file_name]`, file.file_name);
//     formData.append(`empfile[${index}][file_url]`, file.file_url);
//   });

//   return await request.post<BaseResponse<string>, BaseResponse<string>>("/account/empfile", formData, {
//     headers: {
//       'Content-Type': 'multipart/form-data'
//     }
//   });
// }
